package com.wing.system.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.contract.model.entity.ContractCategory;
import com.wing.contract.model.response.ContractCategoryVO;
import com.wing.system.dao.CaseCategoryDao;
import com.wing.system.model.entity.CaseCategory;
import com.wing.system.model.request.CaseCategoryForm;
import com.wing.system.model.response.CaseCategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 案例分类-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class CaseCategoryServiceImpl extends ServiceImpl<CaseCategoryDao, CaseCategory> implements CaseCategoryService {

    @Override
    public int add(CaseCategoryForm caseCategoryForm) {

        //校验

        CaseCategory caseCategory =new CaseCategory();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(CaseCategory.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(caseCategoryForm, caseCategory, copyOptions);

        return baseMapper.insert(caseCategory);
    }

    @Override
    public List<CaseCategoryVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<CaseCategoryVO> pageList4app(Page page, Map<String, Object> params) {
        List<CaseCategoryVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public CaseCategoryVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    @Transactional
    public int enable(Long id) {
        CaseCategoryVO caseCategoryVO=baseMapper.get4appById(id);
        if (caseCategoryVO!=null){
            CaseCategory caseCategory = new CaseCategory();
            caseCategory.setId(id);
            caseCategory.setDeleted(true);
            int updateCount=baseMapper.updateById(caseCategory);
            CaseCategory caseCategoryParent = new CaseCategory();
            caseCategoryParent.setId(caseCategoryVO.getParentId());
            caseCategoryParent.setHasEnableChild(true);
            baseMapper.updateById(caseCategoryParent);
            return updateCount;
        }
        return 0;
    }

    @Override
    @Transactional
    public int disable(Long id) {
        CaseCategoryVO caseCategoryVO=baseMapper.get4appById(id);
        if (caseCategoryVO!=null){
            CaseCategory caseCategory = new CaseCategory();
            caseCategory.setId(id);
            caseCategory.setDeleted(false);
            int updateCount=baseMapper.updateById(caseCategory);
            int count=baseMapper.selectCount(new QueryWrapper<CaseCategory>().eq("parent_id",caseCategory.getParentId()).eq("deleted",true));
            if (count==0){
                CaseCategory caseCategoryParent=new CaseCategory();
                caseCategoryParent.setId(caseCategoryVO.getParentId());
                caseCategoryParent.setHasEnableChild(false);
                baseMapper.updateById(caseCategoryParent);
            }
            return updateCount;
        }

        return 0;
    }
}
